package game.view.menu.orderMenu
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import game.UIComponent.Button;
	import game.UIComponent.OrderSquare;
	import game.UIComponent.Panel;
	import game.common.CustomBitmap;
	import game.common.NetConnect;
	import game.common.Reflection;
	import game.controller.GameDispatcher;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.ConfigManager;
	import game.managers.UIManager;

	public class OrderMenu extends Panel
	{
		/**右上方菜单*/
		private var _rmenuBg:CustomBitmap;

		private var _triangleRightBtn:Button;
		private var _triangleLeftBtn:Button;

		private var _clockBtn:MovieClip;
		private var _clockPanel:ClockPanel;

		private var _orderSquare1:OrderSquare;
		private var _orderSquare2:OrderSquare;
		private var _orderSquare3:OrderSquare;

		private var _emptySquareArr:Array=[];
		private var _sourceArr:Array=[null,null,null];
		private var _tween:TweenLite;

		public function OrderMenu()
		{
			init();
		}

		override protected function init():void
		{
			setBgImage("menu3", 296, 135);

			_clockBtn=Reflection.createMovieClip("clockBtn");
			_clockBtn.x=x + 5;
			_clockBtn.y=y + height - 27;
			_clockBtn.stop();
			_clockBtn.addEventListener(MouseEvent.MOUSE_OVER, onClockPlay);
			_clockBtn.addEventListener(MouseEvent.MOUSE_OUT, onClockStop);
			addChild(_clockBtn);


			_triangleRightBtn=new Button("", "triangleRightBtn");
			_triangleRightBtn.name="rightBtn";
			_triangleRightBtn.x=x + 20;
			_triangleRightBtn.y=(y + height) / 2 - 20;
			_triangleRightBtn.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_triangleRightBtn);


			_triangleLeftBtn=new Button("", "triangleLeftBtn");
			_triangleLeftBtn.name="leftBtn";
			_triangleLeftBtn.x=_triangleRightBtn.x;
			_triangleLeftBtn.y=_triangleRightBtn.y;
			_triangleLeftBtn.visible=false;
			_triangleLeftBtn.addEventListener(MouseEvent.CLICK, onClick);
			addChild(_triangleLeftBtn);

			_orderSquare1=new OrderSquare(OrderSquare.HORIZONTAL, "emptyIcon");
			_orderSquare1.x=_triangleLeftBtn.x + 75;
			_orderSquare1.y=this.y + 7;
			_emptySquareArr.push(_orderSquare1);
			addChild(_orderSquare1);

			_orderSquare2=new OrderSquare(OrderSquare.HORIZONTAL, "emptyIcon");
			_orderSquare2.x=_orderSquare1.x + 50;
			_orderSquare2.y=_orderSquare1.y;
			_emptySquareArr.push(_orderSquare2);
			addChild(_orderSquare2);

			_orderSquare3=new OrderSquare(OrderSquare.HORIZONTAL, "emptyIcon");
			_orderSquare3.x=_orderSquare2.x + 50;
			_orderSquare3.y=_orderSquare1.y;
			_emptySquareArr.push(_orderSquare3);
			addChild(_orderSquare3);

			_clockPanel=new ClockPanel();
			_clockPanel.x=_clockBtn.x - 58;
			_clockPanel.y=_clockBtn.y + 30;
			_clockPanel.visible=false;

			addChild(_clockPanel);

		}

		private var _tmp:int=0;
		private var _tmpSquare:OrderSquare;

		
		/**
		 * 设置菜式图标样式和字样
		 * index-初始化时位置,默认为-1,即不是初始化
		 * */
		public function setFoodType(source:String, num:int,index:int=-1):void
		{
			_source = source;
			if (_sourceArr.indexOf(source) != -1)
			{
				//_tmpSquare=_emptySquareArr[_sourceArr.indexOf(source)] as OrderSquare;
				_emptySquareArr.every(checkSquare);
				_tmpSquare.foodNum = _tmpSquare.foodNum + num;
				
			}
			else
			{			
				if(_sourceArr.indexOf(null) != -1){
					if (index != -1) 
					{
						_sourceArr[index] = source;
					}else{
						_sourceArr[_sourceArr.indexOf(null)] = source;
					}
					
				}
//				else
//				{
//					_sourceArr.push(source);
//				}
				_emptySquareArr.every(checkSquare);
				_tmpSquare.firstFoodType=source;
				
				_tmpSquare.foodNum=num;
				//	_tmp++;
				if (_sourceArr.indexOf(null) == -1 && _sourceArr.length == 3)
				{
					Global.foodFull = true;
					//GameDispatcher.getInstance().dispatchEvent(new GameEvent(EventName.MENU_FULL));
					//return;
				}
			}
		}

		
		private function checkSquare(item:*,index:int,arr:Array):Boolean
		{
			if (item.source1 == _source) 
			{
				_tmpSquare=arr[index] as OrderSquare;
				return false;
			}else{
				_tmpSquare=_emptySquareArr[_sourceArr.indexOf(_source)] as OrderSquare;
				return true;
			}
		}
		
		private var _source:String;
		/**
		 *	设置食物数量
		 * */
		public function setFoodNum(source:String):void
		{
			_source = source;
			//_tmpSquare=_emptySquareArr[_sourceArr.indexOf(source)] as OrderSquare;
			_emptySquareArr.every(checkSquare);

			if (_sourceArr.indexOf(source) != -1)
			{
				if (_tmpSquare.foodNum - 1 <= 0) 
				{
					//_tmpSquare=_emptySquareArr[_sourceArr.indexOf(source)] as OrderSquare;
					_tmpSquare.setEmptyFoodType();				
					_tmpSquare.foodNum=0;
				//	Global.foodDataArr[_sourceArr.indexOf(source)].num = _tmpSquare.foodNum;
					
					Global.foodFull = false;
					Global.foodDataArr[_sourceArr.indexOf(source)]=null;
					Global.foodArr[_sourceArr.indexOf(source)]=null;
								
					if (ConfigManager.CONNECT_SERVER) 
					{
						NetConnect.getInstance().call(null,_sourceArr.indexOf(source));
					}
					
					_sourceArr[_sourceArr.indexOf(source)]=null;
					UIManager.menuPanel.checkBuyBtnEnabled();
					
					return;
				}
				
				_tmpSquare.foodNum = _tmpSquare.foodNum-1;
				Global.foodDataArr[_sourceArr.indexOf(source)].num = _tmpSquare.foodNum;
			}
			
		}
		
		private var _tempBtn:Button;
		/**
		 * 设置左右方向三角按钮和缓动
		 * */
		private function onClick(e:MouseEvent):void
		{
			_tempBtn=e.currentTarget as Button;
			_tempBtn.visible=false;

			switch (e.currentTarget.name)
			{
				case "rightBtn":
					_tween=TweenLite.to(this, 0.5, {x: 950});
					_tempBtn=_triangleLeftBtn;
					break;
				case "leftBtn":
					_tween=TweenLite.to(this, 0.5, {x: 710});
					_tempBtn=_triangleRightBtn;
					break;
			}

			_tempBtn.visible=true;
		}
		
		
		private function onClockStop(e:MouseEvent):void
		{
			_clockBtn.gotoAndStop(1);
			_clockPanel.visible=false;
		}

		private function onClockPlay(e:MouseEvent):void
		{
			_clockBtn.play();
			_clockPanel.visible=true;
		}

		public function removeAllEvent():void
		{
			_clockBtn.removeEventListener(MouseEvent.MOUSE_OUT, onClockStop);
			_clockBtn.removeEventListener(MouseEvent.MOUSE_OVER, onClockPlay);

			_triangleLeftBtn.removeEventListener(MouseEvent.CLICK, onClick);
			_triangleRightBtn.removeEventListener(MouseEvent.CLICK, onClick);

		}
	}
}